<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CRUD_Model extends CI_Model
{
	private $table;
	private $result_type;

	function __construct()
	{
		parent::__construct();
	}

	function GetParam($param)
	{
		return $this->$param;
	}

	function SetParam($param, $value)
	{
		return $this->$param = $value;
	}

	private function SendResult($result)
	{
		return $this->result_type != 'array' ? $result->result() : $result->result_array();
	}

	function Insert($data)
	{
		$this->db->insert($this->table, $data);
		return $this->db->insert_id();
	}

	function Update($update_array, $data)
	{
		foreach ($update_array as $key => $value)
			$this->db->where($key, $value);
		$this->db->update($this->table, $data);
	}

	function Delete($delete_array)
	{
        foreach ($delete_array as $key => $value)
			$this->db->where($key, $value);
        $this->db->delete($this->table);
    }

    function Select($select_array)
    {
	    $this->db->select($select_array);
	    $query = $this->db->get($this->table);
	    return $this->SendResult($query);
    }

    function SelectWhere($select_array, $select_where)
    {
    	$this->db->select($select_array);
    	foreach ($select_where as $key => $value)
    		$this->db->where($key, $value);
    	$query = $this->db->get($this->table);
    	return $this->SendResult($query);
    }

    function GetWhere($select_array)
    {
    	foreach ($select_array as $key => $value)
    		$this->db->where($key, $value);
    	$query = $this->db->get($this->table);
    	return $this->SendResult($query);
    }

    function SelectMax($field)
    {
    	$this->db->select_max($field);
    	$query = $this->db->get($this->table);
    	return $this->SendResult($query);
    }

    function SelectMin($field)
    {
    	$this->db->select_min($field);
    	$query = $this->db->get($this->table);
    	return $this->SendResult($query);
    }

    function SelectAvg($field)
    {
    	$this->db->select_avg($field);
    	$query = $this->db->get($this->table);
    	return $this->SendResult($query);
    }

    function SelectSum($field)
    {
    	$this->db->select_sum($field);
    	$query = $this->db->get($this->table);
    	return $this->SendResult($query);
    }

    function JoinTable($select_array, $join, $join_type)
    {
    	$this->db->select($select_array);
    	$this->db->join($this->table, $join, $join_type);
    	$query = $this->db->get($this->table);
    	return $this->SendResult($query);
    }

    /*
     * TODO
     * function Like($select_array, $field)
    {
    	$this->db->select($select_array);
    	
    }*/

}